#include "stdafx.h"
#include "ASN1Sequence.h"

namespace AppliedCryptography
{
	ASN1Sequence::ASN1Sequence(ClassBits classBits, ConstructedBit constucted, TypeNumberBits number)
		: ASN1Encodable(classBits, constucted, number)
	{
	};

	ASN1Sequence::ASN1Sequence()
		: ASN1Encodable(Universal, Constructed, Sequence)
	{
	};

	ASN1Sequence& ASN1Sequence::add(ASN1Encodable& obj)
	{
		//TODO: Should make a copyof passed object by that there won't 
		//      be problems by changing passed object outside container

		// Do not let add() itself
		if(&obj == this)
			return *this;

		getStdContainer().push_back(&obj);

		return *this;
	};

	ASN1Encodable& ASN1Sequence::get(unsigned int index) const
	{
		return *getStdContainer().at(index);
	};

	std::vector<byte> ASN1Sequence::getContentBits() const
	{
		std::vector<byte> buffer;
		std::vector<byte> unit;

		for(unsigned int i=0; i < getStdContainer().size(); ++i)
		{
			unit.clear();
			unit = getStdContainer()[i]->encode();
			buffer.insert(buffer.end(), unit.begin(), unit.end());
		};

		return buffer;
	};
	const std::vector<ASN1Encodable*>& ASN1Sequence::getStdContainer() const
	{
		return _value;
	};

	std::vector<ASN1Encodable*>& ASN1Sequence::getStdContainer()
	{
		return _value;
	};

	unsigned int ASN1Sequence::size() const
	{
		return getStdContainer().size();
	};

	ASN1Sequence ASN1Sequence::getInstance(ASN1Encodable& obj)
	{
		ASN1Sequence seq;

		if(obj.getTypeBits() == seq.getTypeBits())
		{
			return *(ASN1Sequence*)&obj;
		};

		throw std::exception("Unknow object in getInstance()!");
	};

	ASN1Sequence& ASN1Sequence::flush()
	{
		this->getStdContainer().clear();
		return *this;
	};
}; // namespace AppliedCryptography

